package com.wuhao.lib_common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * com.rocketarmyevaluating.Utils
 * Created by Mr.Hao on 2017/6/25 21:03.
 */

public class TimeUtils {
    public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
    public static final String YEAR_MONTH_DAY_HOUR = "yyyy-MM-dd HH";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE = "yyyy-MM-dd HH:mm";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE = "yyyy-MM-dd HH:mm:ss";

    public static final String YEAR_MONTH_DAY_CHINESE = "yyyy年MM月dd日";
    public static final String YEAR_MONTH_DAY_HOUR_CHINESE = "yyyy年MM月dd日HH时";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_CHINESE = "yyyy年MM月dd日HH时mm分";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE_CHINESE = "yyyy年MM月dd日HH时mm分ss秒";

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNowTime(String timeFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
        String nowTime = sdf.format(new Date());
        return nowTime;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE);
        String nowTime = sdf.format(new Date());
        return nowTime;
    }

    public static String getNowTime(TimeType timeType) {
        String type = YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE;
        switch (timeType) {
            case YEAR_MONTH_DAY:
                type = YEAR_MONTH_DAY;
                break;
            case YEAR_MONTH_DAY_HOUR:
                type = YEAR_MONTH_DAY_HOUR;
                break;
            case YEAR_MONTH_DAY_HOUR_MINUTE:
                type = YEAR_MONTH_DAY_HOUR_MINUTE;
                break;
            default:
            case YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE:
                type = YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE;
                break;
            case YEAR_MONTH_DAY_CHINESE:
                type = YEAR_MONTH_DAY_CHINESE;
                break;
            case YEAR_MONTH_DAY_HOUR_CHINESE:
                type = YEAR_MONTH_DAY_HOUR_CHINESE;
                break;
            case YEAR_MONTH_DAY_HOUR_MINUTE_CHINESE:
                type = YEAR_MONTH_DAY_HOUR_MINUTE_CHINESE;
                break;
            case YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE_CHINESE:
                type = YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE_CHINESE;
                break;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        String nowTime = sdf.format(new Date());
        return nowTime;
    }

    public enum TimeType {
        YEAR_MONTH_DAY, YEAR_MONTH_DAY_HOUR, YEAR_MONTH_DAY_HOUR_MINUTE, YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE, YEAR_MONTH_DAY_CHINESE, YEAR_MONTH_DAY_HOUR_CHINESE, YEAR_MONTH_DAY_HOUR_MINUTE_CHINESE, YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE_CHINESE
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNowTime2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String nowTime = sdf.format(new Date());
        return nowTime;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getDayTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY);
        String nowTime = sdf.format(new Date());
        return nowTime;
    }

    public static String getDayTimeMessage() {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY_CHINESE);
        String nowTime = sdf.format(new Date());
        return nowTime;
    }

    public static String getLogTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY_HOUR_CHINESE);
        String nowTime = sdf.format(new Date());
        return nowTime;
    }

    public static boolean isDelete(String graduation) {

        long graduationMill = getTimeToMillis(graduation);
        long nowMill = getTimeToMillis(getNowTime());
        return graduationMill > nowMill;
    }

    /**
     * 获取时间差
     *
     * @param startTime
     * @param stopTime
     * @param isMinute
     * @return
     */
    public static long getTimeDistance(String startTime, String stopTime, boolean isMinute) {
        long halfDay = 1000 * 60 * 60 * 6;
        long allDay = 1000 * 60 * 60 * 12;
        long oneMonth = allDay * 30;
        long startLong = getTimeToMillis(startTime);
        long stopLong = getTimeToMillis(stopTime);
        long distance = stopLong - startLong;
//        long longMinute = 0;
//        if (isMinute) {
//            longMinute = distance / (1 * 1000);
//            Log.e("ZJH", longMinute + "");
//            return longMinute;
//        } else {
//            return longMinute;
//        }

        return distance;
    }

    /**
     * 获取星期几
     *
     * @return
     */
    public static String getNowWeek() {

        String week = "星期日";
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;

        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));//指定年份
        calendar.set(Calendar.MONTH, month - 1);//指定月份 Java月份从0开始算
        int daysCountOfMonth = calendar.getActualMaximum(Calendar.DATE);//获取指定年份中指定月份有几天

        //获取指定年份月份中指定某天是星期几
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));  //指定日

        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 1:
                week = "星期日";
                break;
            case 2:
                week = "星期一";
                break;
            case 3:
                week = "星期二";
                break;
            case 4:
                week = "星期三";
                break;
            case 5:
                week = "星期四";
                break;
            case 6:
                week = "星期五";
                break;
            case 7:
                week = "星期六";
                break;
            default:
                break;
        }
        return week;
    }

    public static boolean isDistance30Days(String startTime, String stopTime) {

        long allDay = 1000 * 60 * 60 * 12;
        long oneMonth = allDay * 30;

        long startLong = getTimeToMillis(startTime);
        long stopLong = getTimeToMillis(stopTime);
        long distance = stopLong - startLong;
        return distance <= oneMonth;
    }

    public static boolean isDistance30Days(long startTime, String stopTime) {

        long allDay = 1000 * 60 * 60 * 12;
        long oneMonth = allDay * 30;

        long startLong = startTime;
        long stopLong = getTimeToMillis(stopTime);
        long distance = stopLong - startLong;
        return distance <= oneMonth;
    }

    /**
     * 计算两个时间点之间间距
     *
     * @param startTime
     * @param stopTime
     * @return
     */
    public static String timeDistance(String startTime, String stopTime) {
        String result = "";
        long second = 1000;//秒
        long oneMin = 1000 * 60;//一分钟
        long oneHour = 1000 * 60 * 60;//一小时
        long halfDay = 1000 * 60 * 60 * 12;//半天
        long allDay = 1000 * 60 * 60 * 24;//一天
        long startLong = getTimeToMillis(startTime);
        long stopLong = getTimeToMillis(stopTime);
        long distance = stopLong - startLong;

        if (distance >= allDay) {
            long day = distance / allDay;
            result = day + "天前";
        } else if (distance >= oneHour) {
            long hour = distance / oneHour;
            result = hour + "小时前";
        } else if (distance >= oneMin) {
            long min = distance / oneMin;
            result = min + "分钟前";
        } else {
            long secon = distance / second;
            result = secon + "秒前";
        }
        return result;
    }

    /**
     * 计算两个时间点之间间距
     *
     * @param startTime
     * @param stopTime
     * @return
     */
    public static String timeDistance(long startTime, String stopTime) {
        String result = "";
        //秒
        long second = 1000;
        //一分钟
        long oneMin = 1000 * 60;
        long oneHour = 1000 * 60 * 60;//一小时
        long halfDay = 1000 * 60 * 60 * 12;//半天
        long allDay = 1000 * 60 * 60 * 24;//一天
        long startLong = startTime;
        long stopLong = getTimeToMillis(stopTime);
        long distance = stopLong - startLong;

        if (distance >= allDay) {
            long day = distance / allDay;
            result = day + "天前";
        } else if (distance >= oneHour) {
            long hour = distance / oneHour;
            result = hour + "小时前";
        } else if (distance >= oneMin) {
            long min = distance / oneMin;
            result = min + "分钟前";
        } else {
            long secon = distance / second;
            result = secon + "秒前";
        }
        return result;
    }

    /**
     * 获取毫秒数
     *
     * @param strTime
     * @return
     */
    public static long getTimeToMillis(String strTime) {
        long returnMillis = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE);
        try {
            Date d = sdf.parse(strTime);
            returnMillis = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnMillis;
    }

    /**
     * 获取毫秒数
     *
     * @param strTime
     * @return
     */
    public static long getTimeToMillisLog(String strTime) {
        long returnMillis = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY_HOUR_MINUTE_CHINESE);
        try {
            Date d = sdf.parse(strTime);
            returnMillis = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnMillis;
    }

    /**
     * 获取毫秒数
     *
     * @param strTime
     * @return
     */
    public static long getGraduateTimeToMillis(String strTime) {
        long returnMillis = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY);
        try {
            Date d = sdf.parse(strTime);
            returnMillis = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnMillis;
    }

    /**
     * 把毫秒数转化为时分秒的格式
     *
     * @param millis
     * @return
     */
    public static String getMillisToHour(long millis, TimeType timeType) {
        String hms = "";
        String type = "";
        switch (timeType) {
            case YEAR_MONTH_DAY:
                type = YEAR_MONTH_DAY;
                break;
            case YEAR_MONTH_DAY_HOUR:
                type = YEAR_MONTH_DAY_HOUR;
                break;
            case YEAR_MONTH_DAY_HOUR_MINUTE:
                type = YEAR_MONTH_DAY_HOUR_MINUTE;
                break;
            default:
            case YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE:
                type = YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE;
                break;
            case YEAR_MONTH_DAY_CHINESE:
                type = YEAR_MONTH_DAY_CHINESE;
                break;
            case YEAR_MONTH_DAY_HOUR_CHINESE:
                type = YEAR_MONTH_DAY_HOUR_CHINESE;
                break;
            case YEAR_MONTH_DAY_HOUR_MINUTE_CHINESE:
                type = YEAR_MONTH_DAY_HOUR_MINUTE_CHINESE;
                break;
            case YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE_CHINESE:
                type = YEAR_MONTH_DAY_HOUR_MINUTE_SECONDE_CHINESE;
                break;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(type);
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        hms = formatter.format(millis);
        return hms;
    }
}
